package com.hengpeng.itfints.service.article.impl;

import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.hengpeng.common.exception.ServiceResult;
import com.hengpeng.common.persist.QueryResult;
import com.hengpeng.common.persist.query.DynamicQuery;
import com.hengpeng.common.persist.query.Projections;
import com.hengpeng.common.persist.query.constants.QueryConstants.QueryType;
import com.hengpeng.itfinbase.en.ArticleGroupType;
import com.hengpeng.itfinbase.persist.Article;
import com.hengpeng.itfinbase.persist.ArticleCategory;
import com.hengpeng.itfinbase.persist.ArticleGroup;
import com.hengpeng.itfinbase.persist.ArticleGroupEx;
import com.hengpeng.itfints.service.BaseService;
import com.hengpeng.itfints.service.article.ArticleService;

@Service
public class ArticleServiceImpl extends BaseService implements ArticleService {
    /**
     * 按分组查询
     */
    @Override
    public QueryResult<Article> findArticleListByGroup(ArticleGroupType type, Integer count, Integer currentPage) {
        DynamicQuery query = new DynamicQuery(Article.class, "a");
        query.createAlias(ArticleGroupEx.class, "age");
        query.createAlias(ArticleGroup.class, "g");
        query.eq("g.articleGroupType", type);
        query.eqProperty("age.groupId", "g.id");
        query.eqProperty("age.articleId", "a.id");
        query.addProjection(Projections.property("a"));
        query.setPageSize(count);
        query.setPageIndex(currentPage);
        query.desc("a.publishTime");
        query.setIsSearchTotalCount(Boolean.TRUE);
        System.out.println(query.getSqlResult().getSql());
        return serviceFactory.getPersistService().findWithPage(Article.class, query);
    }

    public Article getArticle(Long articleId) {
        return serviceFactory.getPersistService().get(Article.class, articleId);
    }

    public List<Article> findArticleListByGroupId(Long groupId, Integer count) {
        DynamicQuery query = new DynamicQuery(Article.class, "a");
        query.createAlias(ArticleGroupEx.class, "age");
        query.eq("age.groupId", groupId);
        query.eqProperty("age.articleId", "a.id");
        query.addProjection(Projections.property("a.*"));
        query.setPageSize(count);
        query.desc("a.publishTime");
        query.setQueryType(QueryType.JDBC);
        return portFactory.remoteQuery(query, Article.class).getResults();
    }

    public QueryResult<Article> findArticleByCategoryId(Long categoryId, Integer count, Integer currentPage) {
        DynamicQuery query = new DynamicQuery(Article.class);
        query.eq("category.id", categoryId);
        query.asc("id");
        query.setPageSize(count);
        query.setPageIndex(currentPage);
        return portFactory.remoteQuery(query, Article.class);
    }

    public List<ArticleCategory> findArticleCategoryAll() {
        DynamicQuery query = new DynamicQuery(ArticleCategory.class);
        query.asc("id");
        return serviceFactory.getPersistService().find(ArticleCategory.class, query);
    }

    @Override
    public ServiceResult<List<Article>> queryList(Integer pageIndex, Integer pageSize) {
        DynamicQuery query = new DynamicQuery(Article.class);
        query.desc("publishTime");
        query.setPageIndex(pageIndex);
        query.setPageSize(pageSize);
        query.setQueryType(QueryType.JDBC);
        query.setIsSearchTotalCount(true);
        QueryResult<Article> requery = serviceFactory.getPersistService().findWithPage(Article.class, query);
        Long count = requery.getTotalCount();
        ServiceResult<List<Article>> serviceResult = new ServiceResult<List<Article>>();// 设置返回值
        serviceResult.setReturnValue(requery.getResults());
        serviceResult.setReturnListSize(count.intValue());
        return serviceResult;
    }

    /**
     * 获得上一条新闻
     */
    public Article getPrevArticle(Date publishTime, ArticleGroupType type) {

        DynamicQuery query = new DynamicQuery(Article.class, "a");
        query.createAlias(ArticleGroupEx.class, "age");
        query.createAlias(ArticleGroup.class, "g");
        query.eq("g.articleGroupType", type);
        query.eqProperty("age.groupId", "g.id");
        query.eqProperty("age.articleId", "a.id");
        query.setPageSize(1);
        query.setPageIndex(1);
        query.asc("a.publishTime");
        query.gt("a.publishTime", publishTime);
        query.addProjection(Projections.property("a"));
        query.setIsSearchTotalCount(Boolean.TRUE);
        System.out.println(query.getSqlResult().getSql());
        Article article = null;
        List<Article> results = portFactory.remoteQuery(query, Article.class).getResults();
        if (results != null && results.size() > 0) {
            article = results.get(0);
        }
        return article;
    }

    /**
     * 获得下一条新闻
     */
    @Override
    public Article getNextArticle(Date publishTime, ArticleGroupType type) {

        DynamicQuery query = new DynamicQuery(Article.class, "a");
        query.createAlias(ArticleGroupEx.class, "age");
        query.createAlias(ArticleGroup.class, "g");
        query.eq("g.articleGroupType", type);
        query.eqProperty("age.groupId", "g.id");
        query.eqProperty("age.articleId", "a.id");
        query.setPageSize(1);
        query.setPageIndex(1);
        query.desc("a.publishTime");
        query.lt("a.publishTime", publishTime);
        query.addProjection(Projections.property("a"));
        query.setIsSearchTotalCount(Boolean.TRUE);
        System.out.println(query.getSqlResult().getSql());
        Article article = new Article();
        List<Article> results = portFactory.remoteQuery(query, Article.class).getResults();
        if (results != null && results.size() > 0) {
            article = results.get(0);
        }
        return article;
    }

    /**
     * 根据类型获得分组
     * 
     * @param type
     * @return
     */
    @Override
    public ArticleGroup getArticleByGroupType(ArticleGroupType type) {
        DynamicQuery query = new DynamicQuery(ArticleGroup.class);
        query.eq("articleGroupType", type);
        List<ArticleGroup> list = serviceFactory.getPersistService().find(ArticleGroup.class, query);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    /**
     * 平台常见问题解释
     * 
     * @return
     */
    @Override
    public Article getArticleLike() {
        DynamicQuery query = new DynamicQuery(Article.class);
        query.like("title", "%常见问题解释%");
        List<Article> list = serviceFactory.getPersistService().find(Article.class, query);
        if (list != null && list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

}
